package com.shsoft.myThread;

import com.shsoft.colorfulcc.MainActivity;
import com.shsoft.common.Constant;
import com.shsoft.entity.CompositeItem;

import org.litepal.LitePal;

import java.util.List;

/**
 * 纯色-复合模式 子线程
 */
public class SingleCompositeThread extends Thread{
    private MainActivity activity;
    private List<CompositeItem> itemList;
    private int index; // 记录处理到哪一条步骤

    public SingleCompositeThread(MainActivity activity) {
        this.activity = activity;
        itemList = LitePal.where("colorMode=?", Constant.COLOR_MODE_SINGLE + "").order("id asc").find(CompositeItem.class);
        index = 0;
    }

    @Override
    public void run() {
        System.out.println("纯色复合模式子线程运行");
        while (activity.isPlaying){
            if(itemList == null || itemList.size() == 0){
                // 一个步骤也没有，什么也不做，空循环
            }else {
                CompositeItem item = itemList.get(index);
                String[] colors = item.getColorNum().split(",");
                if(item.getLightMode() == Constant.LIGHT_MODE_LONG){
                    int bright = item.getArg1();
                    if(bright * 2 > 255){
                        bright = 255;
                    }else {
                        bright = bright * 2;
                    }
                    int interval = item.getArg2() * 100;
                    int offtime = item.getArg3() * 100;
                    int circle = item.getArg4();
                    for (String color : colors) {
                        if(activity.isPlaying){
                            int colorNum = Integer.parseInt(color.replace("#",""));
                            if(colorNum >= activity.colors.size()){ // 如果当前的颜色编号超出了预设颜色的范围，直接跳到下一条
                                continue;
                            }
                            int[] rgb = activity.colors.get(colorNum);
                            for(int j = 0; j < circle; j++){ // 每种颜色循环circle次
                                if(activity.isPlaying){
                                    // 将灯珠的颜色全部换成要显示的颜色
                                    for(int i = 0; i < Constant.LIGHT_COUNT; i++){
                                        activity.lights_4display.put(i,rgb);
                                    }
                                    // 让主线程刷新灯珠区域
                                    activity.alpha = bright;
                                    activity.refreshLightsDiv();
                                    // 睡眠 interval
                                    try {
                                        sleep(interval);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                        break;
                                    }
                                    if(offtime != 0){ // 需要灭灯
                                        int rgb_black[] = new int[]{1,1,1};
                                        for(int m = 0; m < Constant.LIGHT_COUNT; m++){
                                            activity.lights_4display.put(m,rgb_black);
                                        }
                                        // 让主线程刷新灯珠区域
                                        activity.alpha = 250;
                                        activity.refreshLightsDiv();
                                        // 睡眠 interval
                                        try {
                                            sleep(offtime);
                                        } catch (InterruptedException e) {
                                            e.printStackTrace();
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }else if(item.getLightMode() == Constant.LIGHT_MODE_HORSE){
                    int bright = item.getArg1();
                    if(bright * 2 > 255){
                        bright = 255;
                    }else {
                        bright = bright * 2;
                    }
                    int direction = item.getArg2();
                    int speed = item.getArg3() * 10;
                    int circle = item.getArg4();
                    int lightCount = item.getArg5();
                    for (String color : colors) {
                        if (activity.isPlaying) {
                            int colorNum = Integer.parseInt(color.replace("#", ""));
                            if (colorNum >= activity.colors.size()) { // 如果当前的颜色编号超出了预设颜色的范围，直接跳到下一条
                                continue;
                            }
                            int[] rgb = activity.colors.get(colorNum);
                            if (lightCount <= 20) { // 普通模式
                                if (activity.isPlaying) {
                                    // 因为只写入count个灯珠的数据，防止其他灯珠显示乱七八糟的的颜色，所以初始化 lights_4display
                                    for(int i = 0; i < Constant.LIGHT_COUNT; i++){
                                        if(i < lightCount){
                                            activity.lights_4display.put(i,rgb);
                                        }else {
                                            activity.lights_4display.put(i,new int[]{1,1,1});
                                        }
                                    }
                                }
                                for(int j = 0; j < circle * 20; j++){
                                    if(activity.isPlaying){
                                        // 每次把所有的颜色按顺时针或者逆时针变换
                                        if(direction == Constant.CLOCKWISE){ // 顺时针
                                            int[] rgb_tmp = activity.lights_4display.get(Constant.LIGHT_COUNT - 1);
                                            for(int m = (Constant.LIGHT_COUNT - 1); m > 0 ; m--){
                                                activity.lights_4display.put(m,activity.lights_4display.get(m - 1));
                                            }
                                            activity.lights_4display.put(0,rgb_tmp);
                                        }else { // 逆时针
                                            int[] rgb_tmp = activity.lights_4display.get(0);
                                            for(int m = 0; m < (Constant.LIGHT_COUNT - 1) ; m++){
                                                activity.lights_4display.put(m,activity.lights_4display.get(m + 1));
                                            }
                                            activity.lights_4display.put((Constant.LIGHT_COUNT - 1) ,rgb_tmp);
                                        }
                                        // 让主线程刷新灯珠区域
                                        activity.alpha = bright;
                                        activity.refreshLightsDiv();
                                        try {
                                            sleep(speed);
                                        } catch (InterruptedException e) {
                                            e.printStackTrace();
                                            break;
                                        }
                                    }
                                }
                            }else { // 叠加模式，比如count = 25，就是从1-5个灯珠轮流跑马
                                for (int c = 0; c < circle; c++) {  // 根据圈数再循环
                                    for (int j = 1; j <= lightCount - 20; j++) { // 每一圈再从1-(count - 20)开始循环,j就是这个小循环中灯珠的数量
                                        if (activity.isPlaying) {
                                            // 因为只写入count个灯珠的数据，防止其他灯珠显示乱七八糟的的颜色，所以初始化 lights_4display
                                            for (int m = 0; m < Constant.LIGHT_COUNT; m++) {
                                                if (m < j) {
                                                    activity.lights_4display.put(m, rgb);
                                                } else {
                                                    activity.lights_4display.put(m, new int[]{1, 1, 1});
                                                }
                                            }
                                            for (int k = 0; k < 20; k++) {
                                                if (activity.isPlaying) {
                                                    // 每次把所有的颜色按顺时针或者逆时针变换
                                                    if (direction == Constant.CLOCKWISE) { // 顺时针
                                                        int[] rgb_tmp = activity.lights_4display.get(Constant.LIGHT_COUNT - 1);
                                                        for (int m = (Constant.LIGHT_COUNT - 1); m > 0; m--) {
                                                            activity.lights_4display.put(m, activity.lights_4display.get(m - 1));
                                                        }
                                                        activity.lights_4display.put(0, rgb_tmp);
                                                    } else { // 逆时针
                                                        int[] rgb_tmp = activity.lights_4display.get(0);
                                                        for (int m = 0; m < (Constant.LIGHT_COUNT - 1); m++) {
                                                            activity.lights_4display.put(m, activity.lights_4display.get(m + 1));
                                                        }
                                                        activity.lights_4display.put((Constant.LIGHT_COUNT - 1), rgb_tmp);
                                                    }
                                                    // 让主线程刷新灯珠区域
                                                    activity.alpha = bright;
                                                    activity.refreshLightsDiv();
                                                    try {
                                                        sleep(speed);
                                                    } catch (InterruptedException e) {
                                                        e.printStackTrace();
                                                        break;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }else if(item.getLightMode() == Constant.LIGHT_MODE_FLOWING_WATER){
                    int bright = item.getArg1();
                    if(bright * 2 > 255){
                        bright = 255;
                    }else {
                        bright = bright * 2;
                    }
                    int direction = item.getArg2();
                    int speed = item.getArg3() * 10;
                    int circle = item.getArg4();
                    int mode = item.getArg5();
                    for (String color : colors) {
                        if(activity.isPlaying){
                            int colorNum = Integer.parseInt(color.replace("#", ""));
                            if (colorNum >= activity.colors.size()) { // 如果当前的颜色编号超出了预设颜色的范围，直接跳到下一条
                                continue;
                            }
                            int[] rgb = activity.colors.get(colorNum);
                            // 因为只写入若干个灯珠的数据，防止其他灯珠显示乱七八糟的的颜色，所以初始化 lights_4display
                            for(int i = 0; i <Constant.LIGHT_COUNT ; i++){
                                activity.lights_4display.put(i,new int[]{1,1,1});
                            }
                            for(int j = 0; j < circle; j++) { // 重复执行circle次
                                if(activity.isPlaying){
                                    activity.alpha = bright;
                                    for(int m = 0; m < 40; m++){
                                        // 让主线程刷新灯珠区域
                                        activity.refreshLightsDiv();
                                        if(direction == Constant.CLOCKWISE) { // 顺时针
                                            if(mode == Constant.FLOWINGWATER_MODE_TAIL_FOLLOW){
                                                if(m < 20){
                                                    activity.lights_4display.put(m, rgb);
                                                }else {
                                                    activity.lights_4display.put((m - 20),new int[]{1,1,1});
                                                }
                                            }else if(mode == Constant.FLOWINGWATER_MODE_HEAD_BACK){
                                                if(m < 20){
                                                    activity.lights_4display.put(m, rgb);
                                                }else {
                                                    activity.lights_4display.put(19 - (m - 20),new int[]{1,1,1});
                                                }
                                            }else if(mode == Constant.FLOWINGWATER_MODE_FULL_FINISH){
                                                if(m == 0){
                                                    for(int i = 0; i <Constant.LIGHT_COUNT ; i++){
                                                        activity.lights_4display.put(i,new int[]{1,1,1});
                                                    }
                                                }
                                                activity.lights_4display.put(m, rgb);
                                                if(m == 19){
                                                    try {
                                                        sleep(speed);
                                                    } catch (InterruptedException e) {
                                                        e.printStackTrace();
                                                        break;
                                                    }
                                                    break;
                                                }
                                            }
                                        }else { // 逆时针
                                            if(mode == Constant.FLOWINGWATER_MODE_TAIL_FOLLOW){
                                                if(m < 20){
                                                    if(m == 0){
                                                        activity.lights_4display.put(0, rgb);
                                                    }else {
                                                        activity.lights_4display.put((20 - m),rgb);
                                                    }
                                                }else {
                                                    if(m == 20){
                                                        activity.lights_4display.put(0, new int[]{1,1,1});
                                                    }else {
                                                        activity.lights_4display.put((40 - m),new int[]{1,1,1});
                                                    }
                                                }
                                            }else if(mode == Constant.FLOWINGWATER_MODE_HEAD_BACK){
                                                if(m < 20){
                                                    if(m == 0){
                                                        activity.lights_4display.put(0, rgb);
                                                    }else {
                                                        activity.lights_4display.put((20 - m),rgb);
                                                    }
                                                }else {
                                                    if(m == 39){
                                                        activity.lights_4display.put(0, new int[]{1,1,1});
                                                    }else {
                                                        activity.lights_4display.put(m - 19,new int[]{1,1,1});
                                                    }
                                                }
                                            }else if(mode == Constant.FLOWINGWATER_MODE_FULL_FINISH){
                                                if(m == 0){
                                                    for(int i = 0; i <Constant.LIGHT_COUNT ; i++){
                                                        activity.lights_4display.put(i,new int[]{1,1,1});
                                                    }
                                                    activity.lights_4display.put(0, rgb);
                                                }else {
                                                    activity.lights_4display.put((20 - m),rgb);
                                                }
                                                if(m == 19){
                                                    try {
                                                        sleep(speed);
                                                    } catch (InterruptedException e) {
                                                        e.printStackTrace();
                                                        break;
                                                    }
                                                    break;
                                                }
                                            }
                                        }
                                        try {
                                            sleep(speed);
                                        } catch (InterruptedException e) {
                                            e.printStackTrace();
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }else if(item.getLightMode() == Constant.LIGHT_MODE_BREATHING){
                    int bright = item.getArg1();
                    int time = item.getArg2();
                    int delay = (time * 1000) / (bright * 2);
                    int circle = item.getArg3();
                    int breathingMode = item.getArg4();
                    for (String color : colors) {
                        if(activity.isPlaying){
                            int colorNum = Integer.parseInt(color.replace("#", ""));
                            if (colorNum >= activity.colors.size()) { // 如果当前的颜色编号超出了预设颜色的范围，直接跳到下一条
                                continue;
                            }
                            int[] rgb = activity.colors.get(colorNum);
                            // 将灯珠的颜色全部换成要显示的颜色
                            for(int i = 0; i < Constant.LIGHT_COUNT; i++){
                                activity.lights_4display.put(i,rgb);
                            }
                            for(int j = 0; j < circle; j++) { // 重复执行circle次
                                if(activity.isPlaying){
                                    if(breathingMode == Constant.BREATHING_MODE_D_L_D){
                                        for (int m = 1; m < (bright * 2); m++){
                                            // 让主线程刷新灯珠区域
                                            if(m <= bright){
                                                activity.alpha = m;
                                            }else {
                                                activity.alpha = bright - (m - bright);
                                            }
                                            if(activity.alpha * 2 > 255){
                                                activity.alpha = 255;
                                            }else {
                                                activity.alpha = activity.alpha * 2;
                                            }
                                            activity.refreshLightsDiv();
                                            try {
                                                sleep(delay);
                                            } catch (InterruptedException e) {
                                                e.printStackTrace();
                                                break;
                                            }
                                        }
                                    }else if(breathingMode == Constant.BREATHING_MODE_L_D_L){
                                        for (int m = 1; m < (bright * 2); m++){
                                            // 让主线程刷新灯珠区域
                                            if(m <= bright){
                                                activity.alpha = (bright + 1) - m;
                                            }else {
                                                activity.alpha = m - (bright - 1);
                                            }
                                            if(activity.alpha * 2 > 255){
                                                activity.alpha = 255;
                                            }else {
                                                activity.alpha = activity.alpha * 2;
                                            }
                                            activity.refreshLightsDiv();
                                            try {
                                                sleep(delay);
                                            } catch (InterruptedException e) {
                                                e.printStackTrace();
                                                break;
                                            }
                                        }
                                    }else if(breathingMode == Constant.BREATHING_MODE_L_D){
                                        for (int m = 1; m <= bright; m++){
                                            // 让主线程刷新灯珠区域
                                            activity.alpha = (bright + 1) - m;
                                            if(activity.alpha * 2 > 255){
                                                activity.alpha = 255;
                                            }else {
                                                activity.alpha = activity.alpha * 2;
                                            }
                                            activity.refreshLightsDiv();
                                            try {
                                                sleep(delay * 2);
                                            } catch (InterruptedException e) {
                                                e.printStackTrace();
                                                break;
                                            }
                                        }
                                    }else if(breathingMode == Constant.BREATHING_MODE_D_L){
                                        for (int m = 1; m <= bright; m++){
                                            // 让主线程刷新灯珠区域
                                            activity.alpha = m;
                                            if(activity.alpha * 2 > 255){
                                                activity.alpha = 255;
                                            }else {
                                                activity.alpha = activity.alpha * 2;
                                            }
                                            activity.refreshLightsDiv();
                                            try {
                                                sleep(delay * 2);
                                            } catch (InterruptedException e) {
                                                e.printStackTrace();
                                                break;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                index++;
                if(index == itemList.size()){
                    index = 0;
                }
            }
        }
    }
}
